<?php
class Myh_View_Helper_Block_Template extends Zend_View_Helper_Abstract
{
	protected $_template;
	
	protected $_view;
	
	protected $_arrInput = array();
	
	public function setTemplate($template, $view=null)
	{
		$path = FONTEND_TEMPLATE;
		if($view == 'admin'){
			$path = ADMIN_TEMPLATE;
		}
		$dirTemplate =  $path . $template;
		if(file_exists($dirTemplate)){
			$this->_template = $dirTemplate;
			require_once $dirTemplate;
		}
	}
	public function getView()
	{
		$this->_view = $this->view;
		return $this->_view;
	}
	public function getTemplate()
	{
		return $this->_template;
	}
	public function addField($name, $type, $options = array())
	{
		$view = $this->getView();
		$type = strtolower($type);
		$inputOptions = array();
		if($options['class']){
			$inputOptions['class']= $options['class'];
		}
		if($options['style']){
			$inputOptions['style']= $options['style'];
		}
		if($options['checked']){
			$inputOptions['checked']= $options['checked'];
		}
		//Input Text
		if($type == 'text'){
			$input = $view->formText($name, $options['value'], $inputOptions);
		}
		
		//Input password
		if($type == 'password'){
			$input = $view->formPassword($name, $options['value'], $inputOptions);
		}
		//Check box
		if($type == 'checkbox'){
			$input = $view->formCheckbox($name, $options['value'], $inputOptions);
		}
		//Signle select
		if($type == 'signselect'){
			$input = $view->formSelect($name, $options['value'], $inputOptions, $options['option']);
		}
		if($type == 'multiselect'){
			foreach($options['select'] as $select){
				$input .= $view->formSelect($select['name'], $select['value'], $inputOptions, $select['option']);
			}
			
		}
		//Captcha field
		if($type=='captcha'){
			$captcha = new Myh_Captcha_Image($options);
			$input = $captcha->render($view);
			$input.= $view->formHidden('captchaID', $captcha->getId());
		}
		$this->_arrInput[] = array(
			'label'		=>$options['label'],
			'input'		=>$input,
			'name'		=>$name,
			'style'		=>$options['style'],
			'comment'	=>$options['comment'],
		);					
		return $this;
	}
	public function addButton($name, $value)
	{
		$view = $this->getView();
		$btn = $view->formSubmit($name, $value);
		return $btn;
	}
	
	public function getFields($template)
	{
		$view = $this->getView();
		$loop =  $view->partialLoop($template, $this->_arrInput);
		return $loop;
	}
}